package com.tanhua.server.controller;


import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.impl.UserInfoServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息
     * 请求头中携带token，token中带有当前登录用户的ID和mobile
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo , @RequestHeader("Authorization") String token){

        //传递来的userInfo并没有ID的值，id要与User的id对应，从token获取，并向userInfo中设置Id
        userInfo.setId(UserHolder.getUserId());
        //调用service
        userInfoService.save(userInfo);
        //响应
        return ResponseEntity.ok(null);
    }
    /**
     * 上传用户头像
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity Head(@RequestBody MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {
        //从token获取id
        Long userId = UserHolder.getUserId();
        //调用Service
        userInfoService.update(headPhoto,userId);

        return ResponseEntity.ok(null);
    }
}
